package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.StudentSignIn;
import org.example.commonapi.domain.TaskSignin;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.dto.StudentSignInDto;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.StringUtil;
import org.example.course.service.StudentSignInService;
import org.example.course.service.TaskSigninService;
import org.example.course.until.CourseUntil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/course")
@Slf4j
public class TaskSignInController {

    @Resource
    private TaskSigninService taskSigninService;

    @Resource
    private StudentSignInService studentSignInService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 添加签到
     * @param taskSignin
     * @return
     */
    @PostMapping("/insert/task/signin")
    public R insertSignIn(@RequestBody TaskSignin taskSignin){
        if(taskSignin.getType()==2)
            taskSignin.setSignInNo(CourseUntil.createInviteCode());
        taskSigninService.save(taskSignin);
        return R.success();
    }

    /**
     * 获取签到信息
     * @param taskId
     * @return
     */
    @GetMapping("/get/task/signin/{taskId}")
    public R getSignIn(@PathVariable Long taskId){
        LambdaQueryWrapper<TaskSignin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskSignin::getTaskId,taskId);
        TaskSignin taskSignin=taskSigninService.getOne(queryWrapper);
        return R.success(taskSignin);
    }

    /**
     * 学生签到成功
     * @param studentSignInDto
     * @param request
     * @return
     */
    @PostMapping("/insert/task/student/signin")
    public R insertStudentSignIn(@RequestBody StudentSignInDto studentSignInDto, HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        studentSignInDto.setUserId(loginInfo.getId());
        LambdaQueryWrapper<StudentSignIn> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(StudentSignIn::getUserId,loginInfo.getId()).eq(StudentSignIn::getSignId,studentSignInDto.getSignId());
        if(studentSignInService.exists(queryWrapper2))return R.error("您已经签到过了");
        if(studentSignInDto.getType()==2) {
            if(StringUtil.isAnyEmpty(studentSignInDto.getSignInNo()))return R.error("签到码不能为空");
            LambdaQueryWrapper<TaskSignin> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(TaskSignin::getId,studentSignInDto.getSignId());
            TaskSignin taskSignin=taskSigninService.getOne(queryWrapper);
            log.info("签到码{}",taskSignin.getSignInNo());
            log.info("学生输入的签到码{}",studentSignInDto.getSignInNo());
            log.info("签到码是否相等{}",taskSignin.getSignInNo().equals(studentSignInDto.getSignInNo()));
            if(!taskSignin.getSignInNo().equals(studentSignInDto.getSignInNo()))
                return R.error("签到码错误");
        }
        studentSignInService.save(new StudentSignIn(studentSignInDto));
        return R.success();
    }

}
